package com.ruoyi.walmart.service.auth.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.Platform;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.Vendor;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.walmart.mapper.auth.PlatformMapper;
import com.ruoyi.walmart.service.auth.PlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PlatformServiceImpl implements PlatformService {

    @Autowired
    PlatformMapper platformMapper;

    @Override
    @DataScope(PlatformAlias="p")
    public List<Platform> selectAllList(Platform platform) {
        return platformMapper.selectAllList(platform);
    }

    @Override
    public List<TreeSelect> selectPlatformTreeList(Platform platform) {
        List<Platform> platformList = SpringUtils.getAopProxy(this).selectAllList(platform);
        return buildPlatformTreeSelect(platformList);
    }

    @Override
    public List<Platform> buildPlatformTree(List<Platform> platformList) {
        List<Platform> returnList = new ArrayList<>();
        List<Long> collect = platformList.stream().map(Platform::getShopId).collect(Collectors.toList());
        for (Platform platform : platformList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(platform.getParentId())){
                recursionFn(platformList,platform);
                returnList.add(platform);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = platformList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildPlatformTreeSelect(List<Platform> platformList) {
        List<Platform> platformTrees = buildPlatformTree(platformList);
        return platformTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     * @param platformList 子级
     * @param platform 父级
     */
    private void recursionFn(List<Platform> platformList , Platform platform){
        // 得到子节点列表
        List<Platform> childList = getChildList(platformList, platform);
        platform.setChildrenPlatform(childList);
        for (Platform newPlatform : childList)
        {
            if (hasChild(platformList, newPlatform))
            {
                recursionFn(platformList, newPlatform);
            }
        }
    }

    /**
     * 获得子节点
     * @param platformList
     * @param platform
     * @return
     */
    private List<Platform> getChildList(List<Platform> platformList, Platform platform){
        List<Platform> returnList = new ArrayList<>();
        Iterator<Platform> iterator = platformList.iterator();
        while (iterator.hasNext()){
            Platform n = (Platform) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == platform.getShopId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Platform> platformList, Platform platform)
    {
        return getChildList(platformList, platform).size() > 0;
    }
}
